package com.doctor.ui.office;

import com.doctor.adapter.SubMenuAdapter;
import com.doctor.comm.FragmentHelper;
import com.doctor.ui.BaseFragmentActivity;
import com.doctor.ui.R;
import com.doctor.ui.consulting.view.ConsultingActivity;
import com.doctor.ui.homedoctor.HomeDoctorActivity;
import com.doctor.ui.medicalknowledge.KnowledgeActivity;
import com.doctor.ui.office.AccountBookFrag.AccountBookCallback;
import com.doctor.ui.office.BuyDrugsFrag.BuyDrugsCallback;
import com.doctor.ui.office.DrugsInfoFrag.DrugsInfoCallback;
import com.doctor.ui.office.FileStorageFrag.FileStorageCallback;
import com.doctor.ui.office.ImmediateNotesFrag.ImmediateNotesCallback;
import com.doctor.ui.office.MedicalInfoFrag.MedicalInfoCallback;
import com.doctor.ui.office.OtherFrag.OtherCallback;
import com.doctor.ui.office.ScheduleTimeFrag.ScheduleTimeCallback;
import com.doctor.ui.office.TodayEventFrag.TodayEventCallback;
import com.doctor.ui.office.WorkArrangeFrag.WorkArrangeCallback;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class OfficeActivity extends BaseFragmentActivity implements TodayEventCallback,ScheduleTimeCallback,WorkArrangeCallback,
             MedicalInfoCallback,DrugsInfoCallback,ImmediateNotesCallback,FileStorageCallback,AccountBookCallback,BuyDrugsCallback,OtherCallback{

	/**
	 * 无纸办公要事提醒
	 */
	
	private final String[] items={"今日要事","作息时间","上班安排","医疗信息","药品信息","即时记事","文件存储","帐目往来","购药计划","其　　它"};
	private TodayEventFrag todayEventFrag;  //今日要事
	private WorkArrangeFrag workArrangeFrag;  //上班安排
	private ScheduleTimeFrag scheduleTimeFrag;  //作息时间
	private MedicalInfoFrag medicalInfoFrag;    //医疗信息
	private DrugsInfoFrag drugsInfoFrag;        //药品信息
	private ImmediateNotesFrag immediateNotesFrag;    //即时记事
	private FileStorageFrag fileStorageFrag;     //文件存储
	private AccountBookFrag accountBookFrag;     //帐目往来
	private BuyDrugsFrag buyDrugsFrag;           //购药计划
	private OtherFrag otherFrag;                 //其它
	private SubMenuAdapter subMenuAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        setContentView(R.layout.base_activity);
		
		initiaView();
		initiaSubMenu();
		getData();
		
		//通知用户升级新数据
		noticeNewData();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//改变主菜单背景颜色
		officeBtn.setBackgroundResource(R.color.green);
		knowledgeBtn.setBackgroundResource(R.color.light_green);
		consultingBtn.setBackgroundResource(R.color.light_green);
		homeDocBtn.setBackgroundResource(R.color.light_green);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//改变主菜单背景颜色
		officeBtn.setBackgroundResource(R.color.light_green);
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		super.initiaView();
		//隐藏搜索
		searchText.setVisibility(View.GONE);
		searchBtn.setVisibility(View.GONE);
		search.setVisibility(View.INVISIBLE);
	}
	
	
	@Override
	protected void initiaSubMenu() {
		// TODO Auto-generated method stub
		super.initiaSubMenu();
		
		FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
		todayEventFrag=null;
		todayEventFrag=new TodayEventFrag();
		
		if (getSupportFragmentManager().getBackStackEntryCount()<=0) {
			transaction.replace(fragContainer.getId(), todayEventFrag).commit();
		}
		
		subMenuAdapter=new SubMenuAdapter(OfficeActivity.this, items);
		subMenu.setAdapter(subMenuAdapter);
		subMenu.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
				switch (arg2) {
				case 0:	  //今日要事
					if (subMenuAdapter.getSelectedPosition()!=0) {
						if (todayEventFrag==null) {
							todayEventFrag=new TodayEventFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), todayEventFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}
					break;

				case 1:	  //作息时间
					if (subMenuAdapter.getSelectedPosition()!=1) {
						if (scheduleTimeFrag==null) {
							scheduleTimeFrag=new ScheduleTimeFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), scheduleTimeFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}
					break;
					
				case 2:		//上班安排
					if (subMenuAdapter.getSelectedPosition()!=2) {
						if (workArrangeFrag==null) {
							workArrangeFrag=new WorkArrangeFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), workArrangeFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}	
					break;	
				
				case 3:  //医疗信息
					if (subMenuAdapter.getSelectedPosition()!=3) {
						if (medicalInfoFrag==null) {
							medicalInfoFrag=new MedicalInfoFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), medicalInfoFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}
					break;	
					
				case 4:	  //药品信息
					if (subMenuAdapter.getSelectedPosition()!=4) {
						if (drugsInfoFrag==null) {
							drugsInfoFrag=new DrugsInfoFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), drugsInfoFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}
					break;
					
				case 5:		//即时记事	
					if (subMenuAdapter.getSelectedPosition()!=5) {
						if (immediateNotesFrag==null) {
							immediateNotesFrag=new ImmediateNotesFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), immediateNotesFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}	
					break;	
					
				case 6:	  //文件存储
					if (subMenuAdapter.getSelectedPosition()!=6) {
						if (fileStorageFrag==null) {
							fileStorageFrag=new FileStorageFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), fileStorageFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}
					break;	
					
				case 7:		//帐目往来
					if (subMenuAdapter.getSelectedPosition()!=7) {
						if (accountBookFrag==null) {
							accountBookFrag=new AccountBookFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), accountBookFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}	
					break;
					
				case 8:		//购药计划
					if (subMenuAdapter.getSelectedPosition()!=8) {
						if (buyDrugsFrag==null) {
							buyDrugsFrag=new BuyDrugsFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), buyDrugsFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}	
					break;	
					
				case 9:		//其它
					if (subMenuAdapter.getSelectedPosition()!=9) {
						if (otherFrag==null) {
							otherFrag=new OtherFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), otherFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter.setSelectedPosition(arg2);
					}	
					break;
					
				}
			}
		});
	}
	
	@Override
	protected void onHeaderClicked() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onMedicalKnowledgeClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(OfficeActivity.this,KnowledgeActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected void onConsultingClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(OfficeActivity.this,ConsultingActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected void onHomeDoctorClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(OfficeActivity.this,HomeDoctorActivity.class);
		startActivity(intent);
		finish();
	}
	
	/**
	 * 以下的回调方法为fragment退栈时subMenu显示相应的按钮
	 */
	@Override
	public void onTodayEvent() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(0);
	}
	
	@Override
	public void onScheduleTime() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(1);
	}	
	
	@Override
	public void onWorkArrange() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(2);
	}

	@Override
	public void onMedicalInfo() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(3);
	}

	@Override
	public void onDrugsInfo() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(4);
	}

	@Override
	public void onImmediateNotes() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(5);
	}

	@Override
	public void onFileStorage() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(6);
	}

	@Override
	public void onAccountBook() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(7);
	}

	@Override
	public void onBuyDrugs() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(8);
	}

	@Override
	public void onOther() {
		// TODO Auto-generated method stub
		subMenuAdapter.setSelectedPosition(9);
	}
	
}
